package jumpingnotes.restlet;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import jumpingnotes.model.entity.Activity;
import jumpingnotes.model.entity.ActivityMediaItem;
import jumpingnotes.model.entity.ActivityTemplate;
import jumpingnotes.model.entity.ActivityTemplateParam;
import jumpingnotes.model.entity.Audio;
import jumpingnotes.model.entity.Book;
import jumpingnotes.model.entity.BookComment;
import jumpingnotes.model.entity.BookVersion;
import jumpingnotes.model.entity.Chapter;
import jumpingnotes.model.entity.ChapterVersion;
import jumpingnotes.model.entity.Deploy;
import jumpingnotes.model.entity.ErrorReport;
import jumpingnotes.model.entity.Group;
import jumpingnotes.model.entity.GroupTopic;
import jumpingnotes.model.entity.GroupTopicComment;
import jumpingnotes.model.entity.Member;
import jumpingnotes.model.entity.MemberExperience;
import jumpingnotes.model.entity.Message;
import jumpingnotes.model.entity.RecordAlbum;
import jumpingnotes.model.entity.RecordTask;
import jumpingnotes.model.entity.Sync;

import org.hibernate.Hibernate;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonMapping
{
	public static JSONObject map(Group group) throws JSONException
	{
		JSONObject groupJson = new JSONObject();

		groupJson.put("groupId", group.getGroupId());
		if (group.getMember() != null)
			groupJson.put("member.memberId", group.getMember().getMemberId());
		groupJson.put("name", group.getName());
		groupJson.put("intro", group.getIntro());
		groupJson.put("image", group.getImage());
		groupJson.put("tag", group.getTag());
		groupJson.put("publicFlag", group.getPublicFlag());
		groupJson.put("memberCount", group.getMemberCount());
		groupJson.put("createTime", group.getCreateTime());
		groupJson.put("updateTime", group.getUpdateTime());

		return groupJson;
	}

	public static JSONObject map(Member member) throws JSONException
	{
		JSONObject memberJson = new JSONObject();

		memberJson.put("memberId", member.getMemberId());
		memberJson.put("nickName", member.getNickName());
		memberJson.put("email", member.getEmail());
		memberJson.put("aboutMe", member.getAboutMe());
		memberJson.put("birthday", member.getBirthday());
		memberJson.put("gender", member.getGender());
		memberJson.put("locationCity", member.getLocationCity());
		memberJson.put("locationProvince", member.getLocationProvince());
		memberJson.put("locationCountry", member.getLocationCountry());
		memberJson.put("image", member.getImage());
		memberJson.put("imageBig", member.getImageBig());
		memberJson.put("imageSmall", member.getImageSmall());
		memberJson.put("imageSquare", member.getImageSquare());
		memberJson.put("homepage", member.getHomepage());
		memberJson.put("memberIdInvite", member.getMemberIdInvite());
		memberJson.put("accessTime", member.getAccessTime());
		memberJson.put("updateTime", member.getUpdateTime());
		memberJson.put("createTime", member.getCreateTime());
		memberJson.put("lastLoginTime", member.getLastLoginTime());

		return memberJson;
	}

	public static JSONObject map(Book book) throws JSONException
	{
		JSONObject bookJson = new JSONObject();

		bookJson.put("bookId", book.getBookId());
		bookJson.put("name", book.getName());
		bookJson.put("subTitle", book.getSubTitle());
		bookJson.put("abstract", book.getAbstract_());
		bookJson.put("author", book.getAuthor());
		bookJson.put("authorIntro", book.getAuthorIntro());
		bookJson.put("publishPress", book.getPublishPress());
		bookJson.put("publishYear", book.getPublishYear());
		bookJson.put("chapterCount", book.getChapterCount());
		bookJson.put("wordCount", book.getWordCount());
		bookJson.put("completeStatus", book.getCompleteStatus());
		bookJson.put("image", book.getImage());
		bookJson.put("website", book.getWebsite());
		bookJson.put("url", book.getUrl());
		bookJson.put("memberId", book.getMemberId());
		bookJson.put("expectRate", book.getExpectRate());
		bookJson.put("createTime", book.getCreateTime());
		bookJson.put("updateTime", book.getUpdateTime());

		return bookJson;
	}

	public static JSONObject map(Chapter chapter) throws JSONException
	{
		JSONObject chapterJson = new JSONObject();

		chapterJson.put("chapterId", chapter.getChapterId());
		if (chapter.getBook() != null)
			chapterJson.put("book.bookId", chapter.getBook().getBookId());
		chapterJson.put("number", chapter.getNumber());
		chapterJson.put("name", chapter.getName());
		chapterJson.put("abstract", chapter.getAbstract_());
		chapterJson.put("uuid", chapter.getUuid());
		chapterJson.put("memberId", chapter.getMemberId());
		chapterJson.put("createTime", chapter.getCreateTime());
		chapterJson.put("updateTime", chapter.getUpdateTime());

		return chapterJson;
	}

	public static JSONObject map(Audio audio) throws JSONException
	{
		JSONObject audioJson = new JSONObject();

		audioJson.put("audioId", audio.getAudioId());
		audioJson.put("recordTask.recordTaskId", audio.getRecordTask().getRecordTaskId());
		if (audio.getSync() != null)
			audioJson.put("sync.syncId", audio.getSync().getSyncId());
		audioJson.put("type", audio.getType());
		audioJson.put("subtype", audio.getSubtype());
		audioJson.put("name", audio.getName());
		audioJson.put("uuid", audio.getUuid());
		audioJson.put("memberId", audio.getMemberId());
		audioJson.put("collabMemberIds", audio.getCollabMemberIds());
		audioJson.put("rating", audio.getRating());
		audioJson.put("createTime", audio.getCreateTime());
		audioJson.put("ratingTotal", audio.getRatingTotal());
		audioJson.put("ratingCount", audio.getRatingCount());
		audioJson.put("flvAudioId", audio.getFlvAudioId());
		audioJson.put("downloadCount", audio.getDownloadCount());
		
		Sync sync = audio.getSync();
		if (sync != null && Hibernate.isInitialized(sync))
		{
			audioJson.put("sync.uuid", sync.getUuid());
		}
		
		return audioJson;
	}

	public static JSONObject map(Deploy deploy) throws JSONException
	{
		JSONObject deployJson = new JSONObject();

		deployJson.put("deployId", deploy.getDeployId());
		if (deploy.getAudio() != null)
			deployJson.put("audio.audioId", deploy.getAudio().getAudioId());
		deployJson.put("vendor", deploy.getVendor());
		deployJson.put("model", deploy.getModel());
		deployJson.put("deployType", deploy.getDeployType());
		deployJson.put("subtype", deploy.getSubtype());
		deployJson.put("uuid", deploy.getUuid());
		deployJson.put("playCount", deploy.getPlayCount());
		deployJson.put("downloadCount", deploy.getDownloadCount());

		return deployJson;
	}

	public static JSONObject map(RecordTask recordTask) throws JSONException
	{
		JSONObject recordTaskJson = new JSONObject();

		recordTaskJson.put("recordTaskId", recordTask.getRecordTaskId());
		if (recordTask.getRecordAlbum() != null)
			recordTaskJson.put("recordAlbum.recordAlbumId", recordTask.getRecordAlbum().getRecordAlbumId());
		if (recordTask.getChapter() != null)
			recordTaskJson.put("chapter.chapterId", recordTask.getChapter().getChapterId());
		recordTaskJson.put("status", recordTask.getStatus());
		recordTaskJson.put("latestAudioId", recordTask.getLatestAudioId());
		recordTaskJson.put("publishedAudioId", recordTask.getPublishedAudioId());
		recordTaskJson.put("createTime", recordTask.getCreateTime());
		recordTaskJson.put("updateTime", recordTask.getUpdateTime());
		recordTaskJson.put("ratingTotal", recordTask.getRatingTotal());
		recordTaskJson.put("ratingCount", recordTask.getRatingCount());

		return recordTaskJson;
	}

	public static JSONObject map(Activity activity) throws JSONException
	{
		JSONObject activityJson = new JSONObject();

		activityJson.put("activityId", activity.getActivityId());
		activityJson.put("url", activity.getUrl());
		activityJson.put("priority", activity.getPriority());
		activityJson.put("createTime", activity.getCreateTime());

		Member member = activity.getMember();
		if (member != null && Hibernate.isInitialized(member))
		{
			activityJson.put("member.memberId", member.getMemberId());
			activityJson.put("member.nickName", member.getNickName());
		}
		
		ActivityTemplate activityTemplate = activity.getActivityTemplate();
		if (activityTemplate != null && Hibernate.isInitialized(activityTemplate))
		{
			activityJson.put("activityTemplate.activityTemplateId", activityTemplate.getActivityTemplateId());
			activityJson.put("activityTemplate.type", activityTemplate.getType());
			activityJson.put("activityTemplate.content", activityTemplate.getContent());
		}
		
		Set<ActivityTemplateParam> activityTemplateParams = activity.getActivityTemplateParams();
		if (activityTemplateParams != null && Hibernate.isInitialized(activityTemplateParams))
		{
			List<JSONObject> activityTemplateParamJsonList = new ArrayList<JSONObject>();
			for (ActivityTemplateParam activityTemplateParam : activityTemplateParams)
			{
				JSONObject activityTemplateParamJson = JsonMapping.map(activityTemplateParam);
				activityTemplateParamJsonList.add(activityTemplateParamJson);
			}
			activityJson.put("activityTemplateParams", activityTemplateParamJsonList);
		}
		
		Set<ActivityMediaItem> activityMediaItems = activity.getActivityMediaItems();
		if (activityMediaItems != null && Hibernate.isInitialized(activityMediaItems))
		{
			List<JSONObject> activityMediaItemJsonList = new ArrayList<JSONObject>();
			for (ActivityMediaItem activityMediaItem : activityMediaItems)
			{
				JSONObject activityMediaItemJson = JsonMapping.map(activityMediaItem);
				activityMediaItemJsonList.add(activityMediaItemJson);
			}
			activityJson.put("activityMediaItems", activityMediaItemJsonList);
		}
		
		return activityJson;
	}

	public static JSONObject map(ActivityTemplateParam activityTemplateParam) throws JSONException
	{
		JSONObject activityTemplateParamJson = new JSONObject();

		activityTemplateParamJson.put("activityTemplateParamId", activityTemplateParam.getActivityTemplateParamId());
		activityTemplateParamJson.put("name", activityTemplateParam.getName());
		activityTemplateParamJson.put("value", activityTemplateParam.getValue());
		
		return activityTemplateParamJson;
	}
	
	public static JSONObject map(ActivityMediaItem activityMediaItem) throws JSONException
	{
		JSONObject activityMediaItemJson = new JSONObject();

		activityMediaItemJson.put("activityMediaItemId", activityMediaItem.getActivityMediaItemId());
		activityMediaItemJson.put("mimeType", activityMediaItem.getMimeType());
		activityMediaItemJson.put("url", activityMediaItem.getUrl());
		
		return activityMediaItemJson;
	}
	
	public static JSONObject map(GroupTopic groupTopic) throws JSONException
	{
		JSONObject groupTopicJson = new JSONObject();

		groupTopicJson.put("groupTopicId", groupTopic.getGroupTopicId());
		if (groupTopic.getGroup() != null)
			groupTopicJson.put("group.groupId", groupTopic.getGroup().getGroupId());
		if (groupTopic.getMember() != null)
			groupTopicJson.put("member.memberId", groupTopic.getMember().getMemberId());
		groupTopicJson.put("title", groupTopic.getTitle());
		groupTopicJson.put("body", groupTopic.getBody());
		groupTopicJson.put("status", groupTopic.getStatus());
		groupTopicJson.put("commentCount", groupTopic.getCommentCount());
		groupTopicJson.put("createTime", groupTopic.getCreateTime());
		groupTopicJson.put("updateTime", groupTopic.getUpdateTime());

		return groupTopicJson;
	}

	public static JSONObject map(BookComment bookComment) throws JSONException
	{
		JSONObject bookCommentJson = new JSONObject();

		bookCommentJson.put("bookCommentId", bookComment.getBookCommentId());
		if (bookComment.getMember() != null)
			bookCommentJson.put("member.memberId", bookComment.getMember().getMemberId());
		if (bookComment.getBook() != null)
			bookCommentJson.put("book.bookId", bookComment.getBook().getBookId());
		bookCommentJson.put("body", bookComment.getBody());
		bookCommentJson.put("createTime", bookComment.getCreateTime());

		Member member = bookComment.getMember();
		if (member != null && Hibernate.isInitialized(member))
		{
			bookCommentJson.put("member.nickName", member.getNickName());
			bookCommentJson.put("member.image", member.getImage());
		}

		return bookCommentJson;
	}

	public static JSONObject map(ErrorReport errorReport) throws JSONException
	{
		JSONObject errorReportJson = new JSONObject();

		errorReportJson.put("errorReportId", errorReport.getErrorReportId());
		errorReportJson.put("memberId", errorReport.getMemberId());
		errorReportJson.put("type", errorReport.getType());
		errorReportJson.put("targetId", errorReport.getTargetId());
		errorReportJson.put("subject", errorReport.getSubject());
		errorReportJson.put("body", errorReport.getBody());
		errorReportJson.put("status", errorReport.getStatus());
		errorReportJson.put("createTime", errorReport.getCreateTime());

		return errorReportJson;
	}

	public static JSONObject map(GroupTopicComment groupTopicComment) throws JSONException
	{
		JSONObject groupTopicCommentJson = new JSONObject();

		groupTopicCommentJson.put("groupTopicCommentId", groupTopicComment.getGroupTopicCommentId());
		if (groupTopicComment.getGroupTopic() != null)
			groupTopicCommentJson.put("groupTopic.groupTopicId", groupTopicComment.getGroupTopic().getGroupTopicId());
		groupTopicCommentJson.put("member.memberId", groupTopicComment.getMember().getMemberId());
		groupTopicCommentJson.put("groupId", groupTopicComment.getGroupId());
		groupTopicCommentJson.put("body", groupTopicComment.getBody());
		groupTopicCommentJson.put("createTime", groupTopicComment.getCreateTime());

		return groupTopicCommentJson;
	}

	public static JSONObject map(Message message) throws JSONException
	{
		JSONObject messageJson = new JSONObject();

		messageJson.put("messageId", message.getMessageId());
		messageJson.put("memberByMemberIdTo", message.getMemberByMemberIdTo());
		messageJson.put("memberByMemberIdFrom", message.getMemberByMemberIdFrom());
		messageJson.put("createTime", message.getCreateTime());
		messageJson.put("body", message.getBody());
		messageJson.put("subject", message.getSubject());
		messageJson.put("isRead", message.getIsRead());
		messageJson.put("isDeleteTo", message.getIsDeletedTo());
		messageJson.put("isDeleteFrom", message.getIsDeletedFrom());

		return messageJson;
	}

	public static JSONObject map(RecordAlbum recordAlbum) throws JSONException
	{
		JSONObject recordAlbumJson = new JSONObject();

		recordAlbumJson.put("recordAlbumId", recordAlbum.getRecordAlbumId());
		if (recordAlbum.getBook() != null)
			recordAlbumJson.put("book.bookId", recordAlbum.getBook().getBookId());
		if (recordAlbum.getMember() != null)
			recordAlbumJson.put("member.memberId", recordAlbum.getMember().getMemberId());
		recordAlbumJson.put("name", recordAlbum.getName());
		recordAlbumJson.put("intro", recordAlbum.getIntro());
		recordAlbumJson.put("activeLevel", recordAlbum.getActiveLevel());
		recordAlbumJson.put("albumRating", recordAlbum.getAlbumRating());
		recordAlbumJson.put("publicFlag", recordAlbum.getPublicFlag());
		recordAlbumJson.put("collabFlag", recordAlbum.getCollabFlag());
		recordAlbumJson.put("expectRate", recordAlbum.getExpectRate());
		recordAlbumJson.put("createTime", recordAlbum.getCreateTime());
		recordAlbumJson.put("ratingTotal", recordAlbum.getRatingTotal());
		recordAlbumJson.put("ratingCount", recordAlbum.getRatingCount());
		recordAlbumJson.put("publicGroupId", recordAlbum.getPublicGroupId());

		Member member = recordAlbum.getMember();
		if (member != null && Hibernate.isInitialized(member))
		{
			recordAlbumJson.put("member.nickName", member.getNickName());
			recordAlbumJson.put("member.image", member.getImage());
		}

		return recordAlbumJson;
	}

	public static JSONObject map(BookVersion bookVersion) throws JSONException
	{
		JSONObject bookVersionJson = new JSONObject();

		bookVersionJson.put("bookVersionId", bookVersion.getBookVersionId());
		if (bookVersion.getBook() != null)
			bookVersionJson.put("book.bookId", bookVersion.getBook().getBookId());
		bookVersionJson.put("property", bookVersion.getProperty());
		bookVersionJson.put("value", bookVersion.getValue());
		bookVersionJson.put("weight", bookVersion.getWeight());
		bookVersionJson.put("memberId", bookVersion.getMemberId());
		bookVersionJson.put("createTime", bookVersion.getCreateTime());
		bookVersionJson.put("updateTime", bookVersion.getUpdateTime());

		return bookVersionJson;
	}

	public static JSONObject map(ChapterVersion chapterVersion) throws JSONException
	{
		JSONObject chapterVersionJson = new JSONObject();

		chapterVersionJson.put("chapterVersionId", chapterVersion.getChapterVersionId());
		if (chapterVersion.getChapter() != null)
			chapterVersionJson.put("chapter.chapterId", chapterVersion.getChapter().getChapterId());
		chapterVersionJson.put("property", chapterVersion.getProperty());
		chapterVersionJson.put("value", chapterVersion.getValue());
		chapterVersionJson.put("weight", chapterVersion.getWeight());
		chapterVersionJson.put("memberId", chapterVersion.getMemberId());
		chapterVersionJson.put("createTime", chapterVersion.getCreateTime());
		chapterVersionJson.put("updateTime", chapterVersion.getUpdateTime());

		return chapterVersionJson;
	}

	public static JSONObject map(MemberExperience memberExperience) throws JSONException
	{
		JSONObject memberExperienceJson = new JSONObject();

		memberExperienceJson.put("memberExpId", memberExperience.getMemberExpId());
		if (memberExperience.getMember() != null)
			memberExperienceJson.put("member.memberId", memberExperience.getMember().getMemberId());
		memberExperienceJson.put("platformExp", memberExperience.getPlatformExp());
		memberExperienceJson.put("platformLevel", memberExperience.getPlatformLevel());
		memberExperienceJson.put("platformExpTotal", memberExperience.getPlatformExpTotal());
		memberExperienceJson.put("platformTrophy", memberExperience.getPlatformTrophy());
		memberExperienceJson.put("jobExp", memberExperience.getJobExp());
		memberExperienceJson.put("jobLevel", memberExperience.getJobLevel());
		memberExperienceJson.put("jobExpTotal", memberExperience.getJobExpTotal());
		memberExperienceJson.put("jobTrophy", memberExperience.getJobTrophy());

		return memberExperienceJson;
	}

	public static JSONObject map(Class<?> type, Object object) throws JSONException
	{
		JSONObject json = null;

		try
		{
			Method mapMethod = JsonMapping.class.getMethod("map", type);
			json = (JSONObject) mapMethod.invoke(null, object);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return json;
	}
}
